﻿using AutoMapper;
using B.S.DataBase.Myproject.Helper;
using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.Domain.BOM;
using B.S.DataBase.MyProject.Domain.SaleOrders;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using Microsoft.AspNetCore.Http;
using T.E.ERP.Write.API.Applicantions.Command.MyOrderMementCommand;

namespace T.E.ERP.Write.API.Applicantions.CommandHandler.MyOrdermementHandler
{
    /// <summary>
    /// 订单工单基础信息新增命令处理
    /// </summary>
    public class OrdermementHandler : IRequestHandler<OrdermementCommand, APIResult<int>>
    {
        private readonly IBaseRepository<InfrastrucModel> _infrastrucModel;
        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;
        private readonly SDbContext db;
        private readonly IBaseRepository<SystemLog> _systemLog;
        // 日志记录器
        private readonly ILogger<OrdermementHandler> _logger;

        public OrdermementHandler(IBaseRepository<InfrastrucModel> infrastrucModel, ILogger<OrdermementHandler> logger, IMapper mapper, SDbContext db, IBaseRepository<SystemLog> systemLog)
        {
            _infrastrucModel = infrastrucModel;
            _logger = logger;
            this.mapper = mapper;
            this.db = db;
            _systemLog = systemLog;
        }

        public async Task<APIResult<int>> Handle(OrdermementCommand request, CancellationToken cancellationToken)
        {
            var result = new APIResult<int>()
            {
                Msg = APIEnum.添加成功.ToString(),
                Code = APIEnum.添加成功,
            };
            using (var transaction = await db.Database.BeginTransactionAsync(cancellationToken))
            {
                try
                {
                    InfrastrucModel infrastruc = new InfrastrucModel();
                    infrastruc.GetMaintitle = request.GetMaintitle;
                    infrastruc.OrderCode = request.OrderCode;
                    infrastruc.JoinGetOrderid = request.JoinGetOrderid;
                    infrastruc.JoinShouOrderid = request.JoinShouOrderid;
                    infrastruc.SaleDate = request.SaleDate;
                    infrastruc.GongName = request.GongName;
                    infrastruc.LianName = request.LianName;
                    infrastruc.PhoneNumber = request.PhoneNumber;
                    infrastruc.ByName = request.ByName;
                    infrastruc.ByDepart = request.ByDepart;
                    infrastruc.LianPhone = request.LianPhone;
                    infrastruc.JoinProject = request.JoinProject;
                    infrastruc.SuanType = request.SuanType;
                    infrastruc.Address = request.Address;
                    infrastruc.Remember = request.Remember;
                    infrastruc.SumMoney = 10000;
                    infrastruc.YfuMoney = 9000;
                    infrastruc.YdpMoney = 8000;
                    infrastruc.ShenpStuct = States.待审批;
                    infrastruc.FukuanStuct = "未完成支付";
                    infrastruc.RukuStuct = "未完成入库";
                    infrastruc.DpiaoStuct = "未完成到票";
                    var addinfo = await _infrastrucModel.Add(infrastruc);
                    if (addinfo > 0)
                    {
                        result.Code = APIEnum.添加成功;
                        result.Msg = APIEnum.添加成功.ToString();
                        //实例化系统日志表
                        SystemLog systemlog = new SystemLog();
                        systemlog.LoginName = "张靖威";//systemlog.LoginName; //request.LoginName;
                        systemlog.LoginTime = DateTime.Now;
                        systemlog.Dosomething = "成功调用了订单工单新增方法";
                        await _systemLog.Add(systemlog);
                        //提交事务
                        await transaction.CommitAsync(cancellationToken);
                    }
                    else
                    {
                        result.Code = APIEnum.添加失败;
                        result.Msg = APIEnum.添加失败.ToString();
                        //实例化系统日志表
                        SystemLog systemlog = new SystemLog();
                        systemlog.LoginName = "张靖威";//systemlog.LoginName; request.LoginName;"张靖威";
                        systemlog.LoginTime = DateTime.Now;
                        systemlog.Dosomething = "调用订单工单新增方法失败";
                        await _systemLog.Add(systemlog);
                        //回滚事务
                        await transaction.RollbackAsync(cancellationToken);
                    }
                }
                catch (Exception ex)
                {
                    // 输出详细的错误信息
                    string errorMessage = $"添加失败: {ex.Message}";
                    if (ex.InnerException != null)
                    {
                        errorMessage += $" 内部异常: {ex.InnerException.Message}";
                    }
                    result.Code = APIEnum.添加失败;
                    result.Msg = errorMessage;
                    await transaction.RollbackAsync(cancellationToken);
                }
            }

            return await Task.FromResult(result);
        }
    }
}

